// pages/home/home-nav/medical-condition/bmi-control/add-bmi/index.ts
import { formatTime } from '../../../../../../utils/util'
import { addBMI } from '../../../../../../utils/api/BMIControl'
import { getPatientBaseObject } from '../../../../../../utils/api/patientBase'
import { mySleep } from '../../../../../../utils/util'
import userBaseInfoState from '../../../../../../stores/userBaseInfo'
import Notify from '@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: <PatientBaseEntity>{
      patientUid: '',
      wxUid: '',
      physicalStrength: '',
    },
    height: '',
    weight: '',
    date: '2024-07-01',
    minDate: <Date | number | null>null,
    maxDate: <Date | number | null>null,
    calendarShow: false, // 日期弹出框是否展示
    errorMessageHeight: '',
    errorMessageWeight: ''
  },
  updateUserInfo() {
    const data = <PatientBaseEntity>{ wxUid: wx.getStorageSync("openid") };
    // 如果已经获取状态信息那么无需再次获取，判断是否具有 patientUid和username即可确认
    getPatientBaseObject(data).then((res: any) => {
      // console.log('获取基本信息成功-disease-add', res);
      userBaseInfoState.onChangeData(res.data[0])
      this.setData({
        userInfo: userBaseInfoState.data
      })
    }).catch((err: any) => {
      console.log("获取基本信息失败", err);
    })
  },

  addRecord() {
    console.log('确认添加');
    const data = {
      patientUid: this.data.userInfo.patientUid,
      weight: this.data.weight,
      bmimeasurementDate: this.data.date,
      height: this.data.height,
    }
    if (this.data.height === '') {
      Notify({ type: 'danger', message: '请输入您的身高' });
      return;
    }
    let value = parseFloat(this.data.height);
    const max = 300;
    const min = 10;
    // 校验是否符合
    if (!(value < max && value > min)) {
      Notify({ type: 'danger', message: '请检查您输入的身高' });
      return;
    }
    if (this.data.weight === '') {
      Notify({ type: 'danger', message: '请输入您的体重' });
      return;
    }
    value = parseFloat(this.data.weight);
    if (!(value < max && value > min)) {
      Notify({ type: 'danger', message: '请检查您输入的体重' });
      return;
    }
    addBMI(data).then(async (res: any) => {
      console.log('BMI记录上传成功', res);
      await mySleep(500)
      wx.navigateBack({
        delta: 1
      })
    }).catch((err: any) => {
      console.log('BMI记录上传失败', err);
    })
  },
  cancel() {
    this.setData({
      weight: '',
      height: ''
    })
    wx.navigateBack()
  },

  showCalendar() {
    this.setData({
      calendarShow: true
    })
  },
  closeCalendar() {
    this.setData({
      calendarShow: false,
    })
  },
  confirmCalendar(event: any) {
    this.setData({
      calendarShow: false,
      date: formatTime(event.detail, 'yyyy-MM-dd')
    })
  },

  fieldOnChangeHeight(event: any) {
    let value = parseFloat(event.detail)
    if (isNaN(value)) {
      this.setData({
        height: '',
        errorMessageHeight: '输入数值异常'
      })
      return;
    }
    const max = 300;
    const min = 10;
    // 校验是否符合
    if (value < max && value > min) {
      this.setData({
        height: event.detail,
        errorMessageHeight: '',
      })
    } else {
      this.setData({
        height: event.detail,
        errorMessageHeight: '输入身高异常'
      })
    }
  },
  fieldOnChangeWeight(event: any) {
    let value = parseFloat(event.detail)
    if (isNaN(value)) {
      this.setData({
        weight: '',
        errorMessageWeight: '输入数值异常'
      })
      return;
    }
    const max = 200;
    const min = 10;
    // 校验是否符合
    if (value < max && value > min) {
      this.setData({
        weight: event.detail,
        errorMessageWeight: '',
      })
    } else {
      this.setData({
        weight: event.detail,
        errorMessageWeight: '输入体重异常'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.updateUserInfo();
    // 初始化最大最小日期，今天为最大，最小为上2个月
    const maxDate = new Date();
    const minDate = new Date();
    minDate.setMonth(minDate.getMonth() - 1)
    this.setData({
      minDate: minDate.getTime(),
      maxDate: maxDate.getTime(),
      date: formatTime(maxDate, 'yyyy-MM-dd')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})